package cn.lzh.service;
import cn.lzh.entity.Order;
import cn.lzh.entity.Orditem;
import cn.lzh.entity2.ItemBook;
import cn.lzh.entity2.OrdInfo;
import cn.lzh.entity2.OrderLine;
import cn.lzh.mapper.BookMapper;
import cn.lzh.mapper.CartMapper;
import cn.lzh.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;


@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    CartMapper cartMapper;
    @Autowired
    BookMapper bookMapper;

    @Override
    @Transactional
    public void addOrdItem(Order ord, List<Orditem> items) {
        double amt = 0;
        Orditem item;
        //金额=数量+价格
        for(int i=0;i<items.size();i++){
            item=items.get(i);
            amt+=(double) item.getNum() * item.getPrice();
            System.err.println("金额+"+amt);
        }
        //判断一下订单是否添加成功
        if (amt!=0){
            ord.setAmount(amt);
            orderMapper.addOrd(ord);
        }
        int ordid = ord.getOrdid(); //获取 ordid
        if(ordid<1){
            throw new RuntimeException("插入订单失败, 终止事务"); //抛出异常
        }else{

            for (int i = 0; i <items.size() ; i++) {
                item=items.get(i);
                item.setOrdid(ordid);
                Map<String,Object> map = new HashMap<>();
                map.put("bookid",item.getBookid());
                map.put("num",1);
                cartMapper.datee(item.getItemid());//删除购物车
                orderMapper.addItm(item); //添加订单明细
                bookMapper.addFavnum(map); //添加销量数量
            }

        }

    }

    @Override
    public List<OrdInfo> findOrdAndItemsByUid(int uid, int ispay, Date tstart, Date tend) {
       List<OrdInfo> ordInfos=orderMapper.findOrdItemsByUid(uid,ispay,tstart,tend);

        return ordInfos;
    }

    @Override
    public OrdInfo findOrdAndItemsByOrdid(int uid, int ordid) {
        Map<String,Object> map=new HashMap<>();
        map.put("uid",uid);
        map.put("ordid",ordid);
        OrdInfo ordInfo=orderMapper.findOrdItemsByOrdid(map);
        return ordInfo;
    }

    @Override
    public List<Map<String, Object>> findItemInfoByOrdid(int ordid) {
        return orderMapper.findItemInfoByOrdid(ordid);
    }

    @Override
    public void updateOrdAddr(Order ord) {
orderMapper.updateOrdAddr(ord);
    }

    @Override
    public void updateOrdPay(Order ord) {
        orderMapper.updateOrdPay(ord);
    }

    @Override//添加快递
    public void updateOrdShip(Order ord) {

       orderMapper.updateOrdShip(ord);
        System.err.println(ord);
    }

    @Override
    public Order findOrderById(int ordid) {
        return orderMapper.findOrderById(ordid);
    }

    @Override
    public List<ItemBook> findItemBookByOrdid(int ordid) {
        return orderMapper.findItemBookByOrdid(ordid);
    }

    @Override
    public List<OrderLine> findOrdLines(Map<String, Object> map) {

      return orderMapper.findOrdLines(map);


    }

    @Override
    public int getCount(Map<String, Object> map) {
        return orderMapper.getCount(map);
    }




}
